﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyByContact : MonoBehaviour
{
    //行星爆炸效果
    public GameObject explosionAsteroid;
    //玩家爆炸效果
    public GameObject explosionPlayer;
    //声明GameController主体
    private GameController gameController;
    //设置分数
    public int grade = 10;
    // Start is called before the first frame update
    void Start()
    {
        //根据标签找到GameController实体
        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject!=null) { 
            //绑定主体到指定组件
            gameController = gameControllerObject.GetComponent<GameController>();
        }
        else if(gameControllerObject == null){
            Debug.LogError("Cound not find any gameController!");
        }
    }

    void OnTriggerEnter(Collider other)
    {
        //如果碰到了边界
        if (other.gameObject.tag == "Boundary") {
            return;
        }
        //如果陨石碰到了玩家
        if (other.gameObject.tag == "Player")
        {
            GameObject player = Instantiate(explosionPlayer, transform.position, transform.rotation);
            //使用协助线程
            //StartCoroutine(DestroyExplosionEffects(player));
            Destroy(player,2f);
            //陨石碰到玩家后，游戏结束
            gameController.GameOver = true;
        }
        //如果陨石碰到了子弹
        Destroy(other.gameObject);
        Destroy(this.gameObject);
        GameObject asteroid = Instantiate(explosionAsteroid,transform.position,transform.rotation);
        //设置两秒后爆炸特效摧毁
        Destroy(asteroid,2f);
        //使用协助线程后，爆炸效果出不来了
        //StartCoroutine(DestroyExplosionEffects(asteroid));
        //Destroy(asteroid);
        //计分功能
        gameController.AddScore(grade);
    }
    //销毁生成的爆炸特效
    IEnumerator DestroyExplosionEffects(GameObject other) {
        
        new WaitForSeconds(3f);
        Destroy(other);
        yield return new WaitForSeconds(1f);
    }


}
